//
//  GridCellUIView.m
//  iD20
//
//  Created by Andrew Spina on 4/26/10.
//  Copyright 2010 STScI. All rights reserved.
//

#import "GridCellUIView.h"


@implementation GridCellUIView

@synthesize button;
@synthesize controller;
@synthesize token;
@synthesize highlighted;
@synthesize bgcolor;

- (UIButton*) createNewButtonForFrame: (CGRect) frame {
	button = [UIButton buttonWithType:UIButtonTypeCustom];
	button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); // size and position of button
	[button retain];
	[button addTarget:self action:@selector(cellTapped) forControlEvents:UIControlEventTouchDown];
	return button;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
		[self setBackgroundColor:[UIColor clearColor]];
		button = [self createNewButtonForFrame:frame];
		[self addSubview:button];
		imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
		[imageView retain];
		[self addSubview:imageView];
		self.highlighted = NO;
    }
    return self;
}


- (void)drawRect:(CGRect)rect {
	[bgcolor set];
	UIRectFill(CGRectMake(1, 1, self.frame.size.height - 1, self.frame.size.width - 1));
}

- (void) cellTapped {
	// TODO how do I get rid of this warning?
	[self.controller cellTapped: self];
}

- (void) setToken: (NSString *) newToken {
	self->token = newToken;
	if([@"" isEqualToString:token] || token == nil){
		[self->imageView setImage: nil];
	} else {
		UIImage *lImage = [UIImage imageNamed:token];
		self->imageView.image = lImage;
	}
	
	[imageView setNeedsDisplay];
}

- (void) setBgcolor: (UIColor *) color {
	self->bgcolor = color;
	[self setNeedsDisplay];
}

- (void) setHighlighted: (Boolean) highlight {
	self->highlighted = highlight;
	if(highlighted){
		self.bgcolor = [UIColor colorWithRed:.6 green:.6 blue:.8 alpha:.8];
	} else {
		self.bgcolor = [UIColor clearColor];
	}
//	[self setNeedsDisplay];
}

- (void)dealloc {
    [super dealloc];
}


@end
